<?
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/consult/include.php");
IncludeModuleLangFile(__FILE__);

//Проверка прав
$CONS_RIGHT = $APPLICATION->GetGroupRight("consult");
if ($CONS_RIGHT <= "D")
	$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
//Конец проверки прав

$sTableID = "tbl_cons_user";
$oSort = new CAdminSorting($sTableID, "ID", "desc");
$lAdmin = new CAdminList($sTableID, $oSort);

$arFilterRows = array(
		GetMessage("CONS_G_L_FIL_NAME"),
		GetMessage("CONS_G_L_FIL_DATE_CREATE"),
		GetMessage("CONS_G_L_FIL_LAST_ACTIVITY"),
	);

$filter = new CAdminFilter(
	$sTableID."_filter_id",
	$arFilterRows
);

$arFilterFields = Array(
	"find_id",
	"find_name",
	"find_date_create_from",
	"find_date_create_to",
	"find_last_activity_from",
	"find_last_activity_to",
	);

$lAdmin->InitFilter($arFilterFields);

$lAdmin->AddHeaders(array(
	array(	
		"id"		=>"ID",
		"content"	=>"ID",
		"sort"		=>"id",
		"align"		=>"right",
		"default"	=>true,
	),
	array(	
		"id"		=>"NAME",
		"content"	=> GetMessage("CONS_G_L_COL_NAME"),
		"sort"		=>"name",
		"default"	=>true,
	),
	array(	
		"id"		=>"ENTITY_ID",
		"content"	=> GetMessage("CONS_G_L_COL_ENTITY_ID"),
		"sort"		=>"entity",
		"default"	=>true,
	),
	array(	
		"id"		=>"DATE_CREATE",
		"content"	=> GetMessage("CONS_G_L_COL_DATE_CREATE"),
		"sort"		=>"date_create",
		"default"	=>true,
	),
	array(	
		"id"		=>"LAST_ACTIVITY",
		"content"	=> GetMessage("CONS_G_L_COL_LAST_ACTIVITY"),
		"sort"		=>"last_activity",
		"default"	=>true,
	),
));

$cData = new CConsultUser;

//////////////
if($lAdmin->EditAction() && $CONS_RIGHT>="W")
{
	foreach($FIELDS as $ID => $arUpdate)
	{
		if(!$lAdmin->IsUpdated($ID))
			continue;

		$DB->StartTransaction();

		$ID = intval($ID);
		if (!$cData->Update($ID, $arUpdate))
		{
			$lAdmin->AddUpdateError(GetMessage("CONS_G_L_SAVE_ERR")." #".$ID.": ".$cData->LAST_ERROR, $ID);
			$DB->Rollback();
		}
		
		$DB->Commit();
	}
}
if(($arID = $lAdmin->GroupAction()) && $CONS_RIGHT=="W")
{
	foreach($arID as $ID)
	{
		if(strlen($ID)<=0)
			continue;
		$ID = IntVal($ID);

		switch($_REQUEST['action'])
		{
			case "reset_activity":
					$DB->StartTransaction();
					if(!$cData->Update($ID, array("LAST_ACTIVITY"=>"31.12.2007")))
					{
						$DB->Rollback();
						$lAdmin->AddGroupError(GetMessage("CONS_G_L_UPD_ACTIVITY").": ".$cData->LAST_ERROR, $ID);
					}
					$DB->Commit();
				break;
			case "delete":
				$DB->StartTransaction();
					if(!$cData->Delete($ID))
					{
						$DB->Rollback();
						$lAdmin->AddGroupError(GetMessage("CONS_G_L_DEL_ROW"), $ID);
					}
					$DB->Commit();
				break;
		}
	}
}

$arFilter = Array(
	"ID"		=> $find_id,
	"%NAME"	=> $find_name,
	">=DATE_CREATE"	=> $find_date_create_from,
	"<=DATE_CREATE"	=> $find_date_create_to,
	">=LAST_ACTIVITY"	=> $find_last_activity_from,
	"<=LAST_ACTIVITY"	=> $find_last_activity_to,
);

$rsData = $cData->GetList(Array($by=>$order), $arFilter);
$rsData = new CAdminResult($rsData, $sTableID);
$rsData->NavStart();
$lAdmin->NavText($rsData->GetNavPrint(GetMessage("CONS_NAV_USERS")));

while($arRes = $rsData->NavNext(true, "f_")):
	$row =& $lAdmin->AddRow($f_ID, $arRes);

	$entity_str = "";
	if ($arRes["ENTITY_TYPE"] == "U")
		$entity_str = '<a href="user_edit.php?ID='.$arRes["ENTITY_ID"].'">'.$arRes["ENTITY_ID"].'</a>';
	else
	$entity_str = $arRes["ENTITY_ID"];
	
	$row->AddViewField("ENTITY_ID", $entity_str);
	$row->AddInputField ("NAME", Array("size"=>"15"));

	//Контенсное меню НАЧАЛО
	$arActions = array();
	
	if($CONS_RIGHT>="W")
	{
		$arActions[] = array(
			"ICON"=>"delete",
			"TEXT"=> "Delete",
			"ACTION"=>"if(confirm('delete?')) ".$lAdmin->ActionDoGroup($f_ID, "delete"),
		);
	}
	
	$row->AddActions($arActions);
	//Контенсное меню КОНЕЦ
endwhile;

$lAdmin->AddFooter(
	array(
		array("title"=>GetMessage("MAIN_ADMIN_LIST_SELECTED"), "value"=>$rsData->SelectedRowsCount()),
		array("counter"=>true, "title"=> GetMessage("MAIN_ADMIN_LIST_CHECKED"), "value"=>"0"),
	)
);

//Групповые операции

$arB = Array(
	"reset_activity"=>GetMessage("CONS_G_L_RESET_ACTIVITY")
	);

if ($CONS_RIGHT=="W")
	$arB["delete"]=GetMessage("MAIN_ADMIN_LIST_DELETE");

$lAdmin->AddGroupActionTable($arB);
//Групповые операции END

$aContext = array(
	array(
		"ICON" => "btn_new",
		"TEXT"=>GetMessage("MAIN_ADD"),
		"LINK"=>"cons_user_add.php?lang=".LANG,
		"TITLE"=>GetMessage("MAIN_ADD")
	),
);

$lAdmin->AddAdminContextMenu($aContext);

$lAdmin->CheckListMode();

require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/prolog_admin_after.php");?>

<form name="form2" method="GET" action="<?echo $APPLICATION->GetCurPage()?>?">
<?$filter->Begin();?>
<tr>
	<td nowrap>ID:</td>
	<td nowrap><input type="text" name="find_id" value="<?echo htmlspecialchars($find_id)?>" size="10"></td>
</tr>
<tr>
	<td nowrap><?=GetMessage("CONS_G_L_FIL_NAME");?></td>
	<td nowrap><input type="text" name="find_name" value="<?echo htmlspecialchars($find_name)?>" size="44"></td>
</tr>
<tr>
	<td nowrap><?=GetMessage("CONS_G_L_FIL_DATE_CREATE")?>:</td>
	<td nowrap><?echo CalendarPeriod("find_date_create_from", $find_date_create_from, "find_date_create_to", $find_date_create_to, "form2", "Y")?></td>
</tr>
<tr>
	<td nowrap><?=GetMessage("CONS_G_L_FIL_LAST_ACTIVITY")?>:</td>
	<td nowrap><?echo CalendarPeriod("find_last_activity_from", $find_last_activity_from, "find_last_activity_to", $find_last_activity_to, "form2", "Y")?></td>
</tr>
<?$filter->Buttons(array("table_id"=>$sTableID, "url"=>$APPLICATION->GetCurPage(), "form"=>"form2"));?>
<?$filter->End();?>
</form>

<?$lAdmin->DisplayList();?>

<?require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin.php");?>